/*
 * ------------------------------------------------------------------
 * Copyright © 2017 Hangzhou DtDream Technology Co.,Lt d. All rights reserved.
 * ------------------------------------------------------------------
 *       Product:
 *   Module Name:
 *  Date Created: 2023/7/12
 *   Description:
 * ------------------------------------------------------------------
 * Modification History
 * DATE            Name           Description
 * ------------------------------------------------------------------
 * 2023/7/12    小谷 g2038          created
 * ------------------------------------------------------------------
 */
package com.gujh.security.authentication.sms;

import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

public final class SmsAuthenticationConfigurer extends AbstractAuthenticationFilterConfigurer<HttpSecurity, SmsAuthenticationConfigurer, SmsAuthenticationFilter> {

    public SmsAuthenticationConfigurer() {
        super(new SmsAuthenticationFilter(), "/login/sms");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // 添加认证提供者
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        SecurityUserService userDetailService = http.getSharedObject(ApplicationContext.class).getBean(SecurityUserService.class);
        Assert.notNull(userDetailService,"UserDetailsService 不能为空");
        http.authenticationProvider(new SmsAuthenticationProvider(userDetailService));
        // 设置过滤器
        getAuthenticationFilter().setAuthenticationManager(authenticationManager);

        SessionAuthenticationStrategy sessionAuthenticationStrategy =http.getSharedObject(SessionAuthenticationStrategy.class);
        if (sessionAuthenticationStrategy != null) {
            getAuthenticationFilter().setSessionAuthenticationStrategy(sessionAuthenticationStrategy);
        }
        RememberMeServices rememberMeServices = http.getSharedObject(RememberMeServices.class);
        if (rememberMeServices != null) {
            getAuthenticationFilter().setRememberMeServices(rememberMeServices);
        }

        getAuthenticationFilter().setSecurityContextHolderStrategy(this.getSecurityContextHolderStrategy());
        SmsAuthenticationFilter filter = this.postProcess(getAuthenticationFilter());
        http.addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);
    }

    public SmsAuthenticationConfigurer phoneParameter(String phoneParameter) {
        getAuthenticationFilter().setPhoneParameter(phoneParameter);
        return this;
    }

    public SmsAuthenticationConfigurer smsCodeParameter(String smsCodeParameter) {
        getAuthenticationFilter().setSmsCodeParameter(smsCodeParameter);
        return this;
    }

    public SmsAuthenticationConfigurer authenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        getAuthenticationFilter().setAuthenticationSuccessHandler(successHandler);
        return this;
    }

    public SmsAuthenticationConfigurer authenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
        getAuthenticationFilter().setAuthenticationFailureHandler(authenticationFailureHandler);
        return this;
    }

    public void init(HttpSecurity http) throws Exception {
        super.init(http);
        //this.initDefaultLoginFilter(http);
    }

    protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingUrl) {
        return new AntPathRequestMatcher(loginProcessingUrl);
    }
}

